#!/usr/bin/env perl -w
use 5.010;
use strict;

#打开数据文件
if (! open FILE, '<', 'dst/txt_clean.txt') {
	die "File not exits.";
}

#生成解果文件
if (! open RESULT, '>', 'dst/result_txt_clean.txt') {
	die "File not exits.";
}

=pod
#地理级别与文件空格转换关系
my %lev_map = (
	1 => '2',
	3 => '3',
	5 => '4',
	7 => '5'
);
=cut
#当前的级别内容
my @ids = ('0' x 18);
my @leves = (1);
my @names = ('中国人民共合国');

while (<FILE>) {
	#获取上级信息
	my $pre_id = pop(@ids);
	my $pre_leve = pop(@leves);
	my $pre_name = pop(@names);
	#打印上一条信息
	print "$pre_id:$pre_leve:$pre_name\n";

	#获取本条信息
	chomp(my ($id, $lev, $name) = split(/:/));
	#修正id信息
#	$id = sprintf("%-18s",$id);
#	$id =~ s/ /0/g;

	#修正级号信息
#	$lev = $lev_map{$lev};
	#打印本条信息
	print "$id:$lev:$name\n";
	
	#找上一级，等级信息小于本级的
	while ($lev <= $pre_leve) {
		print "$lev : $pre_leve\n";
		$pre_id = pop(@ids);
		$pre_leve = pop(@leves);
		$pre_name = pop(@names);
	}
=pod
	if ($lev == $pre_leve) {
		$pre_id = pop(@ids);
		$pre_leve = pop(@leves);
		$pre_name = pop(@names);
	} elsif ($lev < $pre_leve) {
		#如果级别小于上级
		$pre_id = pop(@ids);
		$pre_leve = pop(@leves);
		$pre_name = pop(@names);
		$pre_id = pop(@ids);
		$pre_leve = pop(@leves);
		$pre_name = pop(@names);
	}
	#如果级别大于上级
	#啥也不做	
	#更新上级信息
	push(@ids, $pre_id);
	push(@leves, $pre_leve);
	push(@names, $pre_name);
=cut
	push(@ids, $pre_id);
	push(@leves, $pre_leve);
	push(@names, $pre_name);

	print "$id:$lev:$name:$pre_id:$pre_leve:$pre_name\n";
	print RESULT "$id:$lev:$name:$pre_id:$pre_leve:$pre_name\n";
	
	#将本次信息更新进去
	push(@ids, $id);
	push(@leves, $lev);
	push(@names, $name);
	print ("-" x 50, "\n");
}

